package com.situ.blog.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.LayUITableResult;
import com.situ.blog.util.Result;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@Scope("prototype")
public class BlogTypeAction extends ActionSupport implements ModelDriven<BlogType> {
    private BlogType blogType = new BlogType();

    @Autowired
    private IBlogTypeService blogTypeService;

    public void selectByPage() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String pageStr = request.getParameter("page");
        int page = 1; // 默认显示第几页数据
        if (!StringUtils.isEmpty(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        int limit = 5; // 默认每页显示多少条记录
        String limitStr = request.getParameter("limit");
        if (!StringUtils.isEmpty(limitStr)) {
            limit = Integer.parseInt(limitStr);
        }
        //组装搜索条件
        String name = request.getParameter("name");
        BlogTypeQuery blogTypeQuery = new BlogTypeQuery(page, limit, name);
        LayUITableResult layUITableResult = blogTypeService.selectByPage(blogTypeQuery);
        JSONUtil.toJSON(layUITableResult);
    }

    public void deleteById() {
        blogTypeService.deleteById(blogType);
        JSONUtil.toJSON(Result.ok("删除成功"));
    }

    public void add() {
        blogTypeService.add(blogType);
        JSONUtil.toJSON(Result.ok("添加成功"));
    }

    @Override
    public BlogType getModel() {
        return blogType;
    }
}
